/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.beans.enterprise;

import ca.trader.comment.Comment;
import ca.trader.comment.CommentEntityConsts;
import ca.trader.except.SaleDoesNotExistException;
import ca.trader.logger.TraderLogger;
import ca.trader.sale.Sale;
import ca.trader.user.User;
import ca.trader.utils.ObjectFactory;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.TypedQuery;

/**
 *
 * @author Ahsan
 */
@Stateless
public class CommentBean implements CommentBeanLocal
{

	 private static final String _className = CommentBean.class.getName();
	 /**
	  * Entity manager for persisting or retrieving sale objects
	  */
	 @PersistenceContext(unitName = TraderPersistenceConsts.PERSISTENCE_UNIT_NAME, type = PersistenceContextType.TRANSACTION)
	 private EntityManager entityManager;
	 
	 @EJB
	 SaleBeanLocal saleBean;

	 @Override
	 @TransactionAttribute(TransactionAttributeType.REQUIRED) 
	 public void insertComment(User commentor, Integer saleID, String commentString) throws SaleDoesNotExistException
	 {

		  String _methodName = "insertSale";

		  TraderLogger.logMethodEntry(_className, _methodName);

		  TraderLogger.logInfo(_className, _methodName, "Attempting comment insert.");

		  // if the sale entity object is null, throw IllegalArgumentException
		  if (commentor == null || saleID == null || commentString == null)
		  {
			   TraderLogger.logWarning(_className, _methodName, "comment string or commentor is null. Exiting");
			   TraderLogger.logMethodExit(_className, _methodName);
			   throw new IllegalArgumentException("comment string or commentor is null");
		  }
		  
		  Sale sale = saleBean.getSaleByID(saleID);
		  
		  Comment comment = ObjectFactory.createComment(commentor, sale, commentString);

		  // persist the sale
		  entityManager.persist(comment);
//		  entityManager.merge(sale);
//		  entityManager.merge(commentor);

		  TraderLogger.logInfo(_className, _methodName, "Successfully inserted new comment into the comment list");

		  TraderLogger.logMethodExit(_className, _methodName);
	 }

	 @Override
	 @TransactionAttribute(TransactionAttributeType.REQUIRED)
	 public List<Comment> getCommentsBySale(Sale sale)
	 {
		  if (sale == null)
		  {
			   TraderLogger.logWarning(_className, "getCommentsBySale", "sale is null");
			   throw new IllegalArgumentException("sale is null");
		  }

		  return getCommentsBySaleID(sale.getId());
	 }

	 @Override
	 @TransactionAttribute(TransactionAttributeType.REQUIRED)
	 public List<Comment> getCommentsBySaleID(Integer saleID)
	 {
		  String _methodName = "getCommentsBySaleID";

		  TraderLogger.logMethodEntry(_className, _methodName);

		  if (saleID == null)
		  {
			   TraderLogger.logWarning(_className, _methodName, "saleID is null");
			   TraderLogger.logMethodExit(_className, _methodName);
			   throw new IllegalArgumentException("saleID is null");
		  }

		  TraderLogger.logInfo(_className, _methodName, String.format("Finding all comments in sale %d", saleID));

		  TypedQuery<Comment> queryCommentsBySaleID = entityManager.createNamedQuery(CommentEntityConsts.NAMED_QUERY_FIND_COMMENTS_BY_SALE, Comment.class);
		  queryCommentsBySaleID.setParameter(CommentEntityConsts.NAMED_QUERY_FIND_COMMENTS_BY_SALE_PARAM_SALE_ID, saleID);

		  TraderLogger.logMethodExit(_className, _methodName);

		  return queryCommentsBySaleID.getResultList();

	 }

	 @Override
	 @TransactionAttribute(TransactionAttributeType.REQUIRED)
	 public List<Comment> getCommentsByUser(User user)
	 {
		  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
	 }

	 @Override
	 @TransactionAttribute(TransactionAttributeType.REQUIRED)
	 public List<Comment> getsCommentsByUserID(Integer userID)
	 {
		  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
	 }

}
